
/***************************************************************************************************************************

This part of the dofile is our code: Create Z, D, Y, and globals per application

***************************************************************************************************************************/

cd "$INDTA"

use anelli_jeea_2020_proprietarydata.dta, clear

***Data prep for main analysis
gen z=admit
			
gen d1=elite
gen d2=graduated

gen y=lwage

gen noweight=1

xi, prefix(" ") noomit i.test_id

global controls		female house commuter test_id_2 test_id_3 test_id_4 test_id_5 test_id_6 enr2005
global weight		noweight
global clustervar	school_year

global controls_s12 female_s1 female_s2 house_s1 house_s2 commuter_s1 commuter_s2 ///
					test_id_2_s1 test_id_2_s2 test_id_3_s1 test_id_3_s2 test_id_4_s1 ///
					test_id_4_s2 test_id_5_s1 test_id_5_s2 test_id_6_s1 test_id_6_s2 ///
					enr2005_s1 enr2005_s2

***Data prep for analysis per x
recode 	female (1=0) (0=1), gen(male)
order 	male, after(female)

assert 	house!=.
sum 	house, detail
local   med=r(p50)
gen 	house_belmed=0, after(house)
replace house_belmed=1 if house<`med'
gen 	house_abvmed=0, after(house) 
replace house_abvmed=1 if house>=`med'

assert 	commuter!=.
gen 	commuter_0=0, after(commuter)
replace commuter_0=1 if commuter==0
gen 	commuter_1=0, after(commuter)
replace commuter_1=1 if commuter==1

gen 	enr2005_0=0 if enr2005!=., after(enr2005)
replace enr2005_0=1 if enr2005==0
gen 	enr2005_1=0 if enr2005!=., after(enr2005)
replace enr2005_1=1 if enr2005==1

gen everyonex3=1
						
global controlsx1  	test_id_1 test_id_2 test_id_3 test_id_4 test_id_5 test_id_6
global controlsx2  	female male house_belmed house_abvmed commuter_0 commuter_1 enr2005_0 enr2005_1
global controlsx3  	everyonex3

global clustervarx 	school_year						

***Keep if vars not missing
keep if y!=. & d1!=. & d2!=. & z!=.



/***************************************************************************************************************************
***Appendix: Replication of Table 4 of Anelli (2020)
***Run before keep if vars!=. for exact results

**Column (2)
eststo: ivregress 2sls y (d1 = z) $controls , vce(cluster $clustervar )
eststo: ivregress 2sls y z $controls if y!=., vce(cluster $clustervar )
eststo: reg d1 z $controls if y!=., vce(cluster $clustervar )

esttab, keep(d1 z) b(3) p(3) nostar nonotes label title("Replication of Table 4 column (2) of Anelli (2020)")
eststo clear

**Column (3)
eststo: rdrobust y score if test_id!=., 		p(0) fuzzy(d1) 	covs($controls) kernel(triangular) bwselect(mserd) vce(cluster $clustervar )
estadd scalar pv=e(pv_rb)	
eststo: rdrobust y score if test_id!=., 		p(0) 			covs($controls) kernel(triangular) bwselect(mserd) vce(cluster $clustervar )
estadd scalar pv=e(pv_rb)	
eststo: rdrobust d1 score if test_id!=. & y!=., p(0) 			covs($controls) kernel(triangular) bwselect(mserd) vce(cluster $clustervar )
estadd scalar pv=e(pv_rb)	

esttab,  b(3) nostar nonotes not noobs label scalars(pv N_h_l N_h_r) title("Replication of Table 4 column (3) of Anelli (2020)")
eststo clear

***Replication of Table 6 of Anelli (2020)

eststo: ivregress 2sls d2 (d1 = z) $controls , vce(cluster $clustervar )

esttab, keep(d1) b(3) p(3) nostar nonotes label title("Replication of Table 6 column (1) of Anelli (2020)")
eststo clear

***************************************************************************************************************************/

